package com.ray.depot.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.auth.system.emun.UseStatus;
import com.ray.depot.service.MaterialPropertyService;
import com.ray.depot.table.customMapper.SelectMaterialMapper;
import com.ray.depot.table.dto.MaterialDto;
import com.ray.depot.table.entity.MaterialProperty;
import com.ray.depot.table.entity.MaterialPropertyExample;
import com.ray.depot.table.entity.MaterialPropertyExample.Criteria;
import com.ray.depot.table.mapper.MaterialPropertyMapper;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
@Service
public class MaterialPropertyServiceImpl extends BaseService implements MaterialPropertyService{
	/**日志对象**/
	private static Logger logger = Logger.getLogger(MaterialPropertyServiceImpl.class);
	
	@Autowired
	private MaterialPropertyMapper materialPropertyMapper;
	
	@Autowired
	private SelectMaterialMapper  selectMaterialMapper;

	@Transactional
	public Result<Object> addMaterialProperty(MaterialProperty params, LoginUser user) {
		//校验
		if (existMaterialNo(params, user, false)) {
			return new Result<Object>(-1, String.format("物料编号：%s 已经存在.", params.getMaterialNo()));
		}
		if (existMnemonicCode(params, user, false)) {
			return new Result<Object>(-1, String.format("助记码：%s 已经存在.", params.getMnemonicCode()));
		}
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		params.setTenantId(user.getTenantId());
		params.setPropertyId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count = materialPropertyMapper.insertSelective(params);
		if(count!=1){
			logger.error("MaterialPropertyServiceImpl=>addMaterialProperty is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public MaterialProperty queryMaterialProperty(String id, LoginUser user) {
		return materialPropertyMapper.selectByPrimaryKey(id);
	}
	
	
    @Transactional
	public Result<Object> updateMaterialProperty(MaterialProperty params, LoginUser user) {
    	//校验
		if (existMaterialNo(params, user, true)) {
			return new Result<Object>(-1, String.format("物料编号：%s 已经存在.", params.getMaterialNo()));
		}
		if (existMnemonicCode(params, user, true)) {
			return new Result<Object>(-1, String.format("助记码：%s 已经存在.", params.getMnemonicCode()));
		}
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=materialPropertyMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("MaterialPropertyServiceImpl=>editMaterialProperty is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional
	public Result<Object> deleteMaterialProperty(String id, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		MaterialProperty params=new MaterialProperty();
		params.setPropertyId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=materialPropertyMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("MaterialPropertyServiceImpl=>deleteMaterialProperty is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<MaterialProperty> queryMaterialProperty(MaterialProperty params, PageDto<MaterialProperty> page, LoginUser user) {
		MaterialPropertyExample example=new MaterialPropertyExample();
		Criteria criteria = example.createCriteria();
		//超级管理员能查看全部
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
		}
		if(StrUtil.isNotBlank(params.getMaterialId())){
			criteria.andMaterialIdEqualTo(params.getMaterialId());
		}
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(materialPropertyMapper.countByExample(example));
		page.setRecords(materialPropertyMapper.selectByExample(example));
		return page;
	}


	@Override
	public void deleteMaterialProprety(String id, LoginUser user) {
		MaterialPropertyExample example=new MaterialPropertyExample();
		Criteria criteria = example.createCriteria();
		criteria.andMaterialIdEqualTo(id);
		MaterialProperty params=new MaterialProperty();
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		materialPropertyMapper.updateByExample(params, example);
	}
	/**
	 * 校验物料编号
	 * @param params
	 * @param user
	 * @param isUpdate
	 * @return
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	private boolean existMaterialNo(MaterialProperty params, LoginUser user, boolean isUpdate) {
		MaterialPropertyExample example = new MaterialPropertyExample();
		Criteria criteria = example.createCriteria();
		criteria.andTenantIdEqualTo(user.getTenantId());
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		criteria.andMaterialNoEqualTo(params.getMaterialNo());
		if(isUpdate){
			criteria.andPropertyIdNotEqualTo(params.getPropertyId());
		}
		long count = materialPropertyMapper.countByExample(example);
		return count != 0;
	}
	
	/**
	 * 校验助记码
	 * @param params
	 * @param user
	 * @param isUpdate
	 * @return
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	private boolean existMnemonicCode(MaterialProperty params, LoginUser user, boolean isUpdate) {
		MaterialPropertyExample example = new MaterialPropertyExample();
		Criteria criteria = example.createCriteria();
		criteria.andTenantIdEqualTo(user.getTenantId());
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		criteria.andMnemonicCodeEqualTo(params.getMnemonicCode());
		if(isUpdate){
			criteria.andPropertyIdNotEqualTo(params.getPropertyId());
		}
		long count = materialPropertyMapper.countByExample(example);
		return count != 0;
	}
	
	 @Transactional(propagation=Propagation.NOT_SUPPORTED)
		public PageDto<MaterialDto> querySelectMaterialProperty(MaterialDto params, PageDto<MaterialDto> page, LoginUser user) {
		    params.setTenantId(user.getTenantId());
		    params.setOrderByClause(page.getOrderBy());
		    params.setLimitStart((page.getPage() - 1)  * page.getPageSize());
		    params.setLimitEnd(page.getPageSize());
		    params.setMaterialStatus(UseStatus.ON.getType());
		    params.setIsDelete(DeleteEnum.USE.getValue());
			page.setTotal(selectMaterialMapper.countByExample(params));
			page.setRecords(selectMaterialMapper.selectByExample(params));
			return page;
		}

}
